package org.chen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.chen.domain.CrawlerTask;
import org.chen.domain.dto.BatchDeleteDTO;
import org.chen.domain.dto.CrawlerStatistics;
import org.chen.domain.dto.CrawlerStatisticsDTO;
import org.chen.domain.response.Result;
import org.chen.domain.vo.ArticleVO;
import org.chen.domain.vo.CrawlerResult;
import org.chen.domain.vo.CrawlerTaskVO;
import org.chen.service.impl.ArticleService;
import org.chen.service.impl.CrawlerTaskService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class CrawlerApiController {

    private final CrawlerTaskService taskService;
    private final ArticleService articleService;

    // 任务相关接口
    @GetMapping("/tasks")
    public Result<IPage<CrawlerTaskVO>> getTasks(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String source,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Boolean scheduled) {
        Page<CrawlerTaskVO> page = taskService.getTaskPage(current, size, source, category, scheduled);
        return Result.success(page);
    }

    @PostMapping("/tasks")
    public Result<CrawlerResult> createTask(@RequestBody CrawlerTask task) {
        CrawlerResult result = taskService.executeTask(task);
        return Result.success(result);
    }

    @GetMapping("/tasks/{taskId}")
    public Result<CrawlerTaskVO> getTaskDetail(@PathVariable String taskId) {
        CrawlerTaskVO task = taskService.getTaskDetail(taskId);
        return Result.success(task);
    }

    @DeleteMapping("/tasks/batch-delete")
    public Result<Void> batchDeleteTasks(@RequestBody BatchDeleteDTO batchDeleteDTO) {
        taskService.batchDelete(batchDeleteDTO);
        return Result.success();
    }

    @DeleteMapping("/tasks/{taskId}")
    public Result<Void> deleteArticles(@PathVariable String taskId) {
        taskService.deleteTaskById(taskId);
        return Result.success();
    }

    // 文章相关接口
    @GetMapping("/articles")
    public Result<IPage<ArticleVO>> getArticles(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String source,
            @RequestParam(required = false) String categoryId,
            @RequestParam(required = false) String keyword) {
        Page<ArticleVO> page = articleService.getArticlePage(current, size, source, categoryId, keyword);
        return Result.success(page);
    }

    @GetMapping("/articles/{id}")
    public Result<ArticleVO> getArticleDetail(@PathVariable String id) {
        ArticleVO article = articleService.getArticleDetail(id);
        return Result.success(article);
    }

    @DeleteMapping("/articles/delete/{id}")
    public Result<Void> deleteArticle(@PathVariable String id) {
        articleService.deleteArticle(id);
        return Result.success();
    }

    @DeleteMapping("/articles/batch-delete")
    public Result<Void> batchDeleteArticles(@RequestBody BatchDeleteDTO batchDeleteDTO) {
        articleService.batchDelete(batchDeleteDTO);
        return Result.success();
    }

    // 统计接口
    @GetMapping("/statistics")
    public Result<CrawlerStatistics> getStatistics() {
        CrawlerStatistics statistics = taskService.getStatistics();
        return Result.success(statistics);
    }

    @GetMapping("/statistics-dashboard")
    public CrawlerStatisticsDTO getStatisticsDashboard() {
//        System.out.println("请求到达了: CrawlerController");
//        CrawlerStatisticsDTO statisticsDashboard = taskService.getStatisticsDashboard();
//        System.out.println(statisticsDashboard);
        return taskService.getStatisticsDashboard();
    }

    @GetMapping("/tasks/recent")
    public List<CrawlerTaskVO> getRecentTasks(@RequestParam(defaultValue = "5") int limit) {
//        System.out.println("请求到达了: CrawlerController");
//        List<CrawlerTaskVO> recentTasks = taskService.getRecentTasks(limit);
//        System.out.println(recentTasks);
        return taskService.getRecentTasks(limit);
    }

    @GetMapping("/tasks/trend")
    public Map<String, Integer> getTaskTrend(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime endTime) {
//        System.out.println("请求到达了: CrawlerController");
//        Map<String, Integer> taskTrend = taskService.getTaskTrend(startTime, endTime);
//        System.out.println(taskTrend);
        return taskService.getTaskTrend(startTime, endTime);
    }
}